#!/bin/bash
clear
RED="\033[31m"    # Error message
GREEN="\033[32m"  # Success message
YELLOW="\033[33m" # Warning message
BLUE="\033[36m"   # Info message
RESET='\033[0m'

if [ "$EUID" -ne 0 ]; then
  echo -e "${RED} Anda tiada kebenaran untuk menjalankan skrip ini! ${RESET}"
  exit 1
fi

apt-get -qq update
apt-get -y -qq install wireguard

mkdir /etc/wireguard/clients
touch /etc/wireguard/clients/.accounts

# generate new private key & public key for wireguard server
wg genkey | tee /etc/wireguard/privatekey | wg pubkey | tee /etc/wireguard/publickey

private_network='10.1.0.1/24'
listen_port='51820'
server_private_key=$(cat /etc/wireguard/privatekey)
server_public_key=$(cat /etc/wireguard/publickey)
public_network_interfaces=$(ip -o -4 route show to default | awk '{print $5}')

# create wireguard config file (/etc/wireguard/wg0.conf)
echo "[Interface]
Address = $private_network
SaveConfig = true
ListenPort = $listen_port
PrivateKey = $server_private_key" >/etc/wireguard/wg0.conf

# set permisson for private key file and wireguard config file
chmod 600 /etc/wireguard/privatekey
chmod 600 /etc/wireguard/wg0.conf

# enable wireguard service on system startup
systemctl enable wg-quick@wg0

# start wireguard service
wg-quick up wg0

echo
echo -e "${GREEN} Pemasangan tinyproxy pakej telah selesai. ${RESET}"
echo
